package com.atguigu.educenter.service.impl;

import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.LoginVo;
import com.atguigu.commonutils.vo.commentVo.CommentMemberVo;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.IUcenterMemberService;
import com.atguigu.utils.JwtUtils;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author AIR
 * @since 2022-10-07
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements IUcenterMemberService {

    //注入redis
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //登录的方法
    @Override
    public String login(LoginVo loginVo) {

        String emailVo = loginVo.getEmail();
        String passwordVo = loginVo.getPassword();

        //邮箱账号和密码非空判断
        if ( StringUtils.isBlank(emailVo) || StringUtils.isBlank(passwordVo)) {
            throw new GuliException(20001, "登录失败！（邮箱或密码为空）");
        }

        //判断邮箱账号是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", emailVo);
        UcenterMember member = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (member == null) {//没有这个邮箱
            throw new GuliException(20001, "您输入的邮箱不存在！");
        }

        //判断密码
        if (!MD5.encrypt(passwordVo).equals(member.getPassword())) {
            throw new GuliException(20001, "您输入的密码不存在！");
        }

        //判断用户是否禁用
        if (member.getIsDisabled()) {
            throw new GuliException(20001, "您的账号已被禁用！");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());

        return jwtToken;
    }

    //注册的方法
    @Override
    public void registerUser(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String email = registerVo.getEmail(); //邮箱
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空判断
        if (
                StringUtils.isBlank(email) || StringUtils.isBlank(password)
                        || StringUtils.isBlank(code) || StringUtils.isBlank(nickname)
        ) {
            throw new GuliException(20001, "注册失败！（未完整输入用户信息）");
        }

        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(email);

        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "验证码错误！");
        }

        //判断邮箱是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        Long count = baseMapper.selectCount(wrapper);
        System.out.println(count);
        if (count > 0) {
            throw new GuliException(20001, "您的邮箱已经确认了1个学院账号，达到了邮箱注册确认上限");
        }

        //加密密码
        String MD5Pwd = MD5.encrypt(password);
        registerVo.setPassword(MD5Pwd);

        //数据添加到数据库中
        UcenterMember member = new UcenterMember();
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("guangzhou.myqcloud.com/img/2022/10/7/61ffaa0686dc44eeb3b8dfe52d12d877.png");

        BeanUtils.copyProperties(registerVo, member);
        System.out.println(member);
        baseMapper.insert(member);
    }

    //根据openid查询
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);

        return member;
    }

    //根据token获取用户信息
    @Override
    public CommentMemberVo getCommentUserInfo(String memberId) {
        System.out.println(memberId);
        UcenterMember member = baseMapper.selectById(memberId);
        System.out.println(member);
        CommentMemberVo memberVo = new CommentMemberVo();
        BeanUtils.copyProperties(member,memberVo);
        System.out.println(memberVo);
        return memberVo;
    }

    //查询某一天的注册人数
    @Override
    public Integer countRegisterDay(String day) {

        return baseMapper.countRegisterDay(day);
    }


}
